package part3.c14_template;

/**
 * Created by lie on 2018/3/23.
 * 封存了冒泡排序的内核
 * 而且可以夹带自己的私货{@link #operations}
 */
public abstract class ABubbleSorter {
    private int operations;
    //length交给派生类去定义
    protected int length;

    protected int doSort(){
        operations = 0;
        if (length <= 1) {
            return operations;
        }

        for (int lastIndex = length-2;lastIndex>=0;lastIndex--){
            for (int begin=0; begin<=lastIndex; begin++){
                //完全省去细节，不管背后数据是什么类型，是什么结构
                if (isNeedSwap(begin))
                    swap(begin);
                operations++;
            }
        }

        return operations;
    }

    protected abstract boolean isNeedSwap(int index);
    protected abstract void  swap(int index);



}
